package com.mujin.keji.collectionsystem.http;

import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;
import com.mujin.keji.collectionsystem.base.BaseVo;
import com.mujin.keji.collectionsystem.utils.HttpUtil;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;

/**
 * ok go 网络请求的封装
 * @author weishixiong
 * @Time 2018-03-20
 */
public class Http {
    private static final String TAG = "Http";
    private static Http instance;

    private Http() {

    }

    public static Http getInstance() {
        if (instance == null) {
            instance = new Http();
        }
        return instance;
    }

    /**
     * post请求
     *
     * @param url
     * @param vo
     * @param listener
     */
    public void postAsynHttp(String url, BaseVo vo, final ResponseListener listener) {
        PostRequest<String> s = OkGo.<String>post(url);//
        // s.headers("header1", "headerValue1")//设置请求头
        Map<String, String> maps = HttpUtil.convertVo2Params(vo);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            s.params(key, value);//
        }
        s.isMultipart(true)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //注意这里已经是在主线程了
                        String data = response.body();//这个就是返回来的结果
                        listener.onSuccess(data);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        listener.onError(response.body());
                    }
                });
    }

    /**
     * get请求
     *
     * @param url
     * @param listener
     */
    public void getAsynHttp(BaseVo vo, String url, final ResponseListener listener) {
        GetRequest<String> s = OkGo.<String>get(url);//
        // s.headers("header1", "headerValue1")//设置请求头
        Map<String, String> maps = HttpUtil.convertVo2Params(vo);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            s.params(key, value);//
        }
        s.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                //注意这里已经是在主线程了
                String data = response.body();//这个就是返回来的结果
                listener.onSuccess(data);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                listener.onError(response.body());
            }
        });
    }


    private void postAsynFile(BaseVo vo, String path, String url, final UpLoadListener listener) {

    }

    /**
     * 上传携带参数的文件
     * @param files
     * @param url
     * @param vo
     * @param listener
     * @param
     */
    public void sendMultipart(List<File> files, String url, BaseVo vo, final UpLoadListener listener) {
        PostRequest<String> s = OkGo.post(url);//
        Map<String, String> maps = HttpUtil.convertVo2Params(vo);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            s.params(key, value);//
        }

        s.addFileParams("uFile", files)           // 这种方式为同一个key，上传多个文件
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                       // Log.d(TAG, "onStart: 正在上传中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        //Logger.d(TAG, "onSuccess: " + "上传完成" + response.body());
                        listener.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        //Logger.d(TAG, "onError: " + "上传出错");
                        listener.onError(response.body());
                    }

                    @Override
                    public void uploadProgress(Progress progress) {
                       // Logger.d(TAG, "uploadProgress: " + progress);
                        listener.onProgres(progress);
                    }
                });
    }

    public interface ResponseListener {
        void onSuccess(String result);

        void onError(String msg);
    }

    public interface UpLoadListener {
        void onSuccess(String result);

        void onProgres(Progress progress);

        void onError(String msg);
    }
}
